Skip to content

Preserve heredoc bodies for shell interpreters in deny checks#5

Merged
jim80net merged 1 commit intofix/strip-heredocs-before-matchingfrom
claude/fix-heredoc-deny-bypass-bB4Xk
Mar 7, 2026
Merged

Preserve heredoc bodies for shell interpreters in deny checks#5
jim80net merged 1 commit intofix/strip-heredocs-before-matchingfrom
claude/fix-heredoc-deny-bypass-bB4Xk

Conversation

@jim80net
Copy link
Copy Markdown
Owner

@jim80net jim80net commented Mar 7, 2026

Summary

Modified the StripHeredocs function to preserve heredoc bodies when they are fed as stdin to shell interpreters (bash, sh, python, etc.), while continuing to strip heredoc bodies for other commands like cat. This ensures that deny rules can properly inspect executable code passed to interpreters.

Key Changes

  • Added shellHeredocRe regex pattern to detect when heredocs are piped to shell interpreters (bash, sh, dash, zsh, ksh, fish, python, ruby, perl, node, php)
  • Modified StripHeredocs to conditionally preserve heredoc bodies based on whether they're being fed to a shell interpreter
  • Added keepBody flag to track whether the current heredoc body should be preserved or stripped
  • Updated test cases to verify:
    • Heredocs for shell interpreters (bash, sh, python) are preserved
    • Heredocs for cat command are still stripped
    • Deny rules properly catch dangerous patterns within preserved heredocs (rm -rf, git reset --hard, DROP TABLE, etc.)

Implementation Details

The solution distinguishes between two types of heredocs:

  1. Data heredocs (e.g., cat <<EOF) - bodies are stripped since they contain data, not executable code
  2. Code heredocs (e.g., bash <<EOF) - bodies are preserved since they contain executable code that deny rules must inspect

The shellHeredocRe pattern matches heredoc syntax immediately following shell interpreter commands, accounting for command chaining with ;, &, and | operators.

https://claude.ai/code/session_017iMvRYni3dU8UACw1Jok9c

StripHeredocs now preserves heredoc bodies when the heredoc is fed as
stdin to a shell interpreter (bash, sh, python, ruby, perl, node, etc.).
Previously, all heredoc bodies were stripped before deny rule matching,
which allowed bypassing deny rules via e.g. `bash <<'EOF'\nrm -rf /\nEOF`.

Non-interpreter heredocs (cat, git commit messages, PR descriptions) are
still stripped as before to avoid false positives on data content.

https://claude.ai/code/session_017iMvRYni3dU8UACw1Jok9c
@jim80net jim80net merged commit 403939e into fix/strip-heredocs-before-matching Mar 7, 2026
1 check failed
@jim80net jim80net deleted the claude/fix-heredoc-deny-bypass-bB4Xk branch March 7, 2026 06:30
jim80net added a commit that referenced this pull request Mar 8, 2026
Preserve heredoc bodies for shell interpreters in deny checks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants